package demo1.CommandPattern;

/**
 * @ClassName CommandPattern
 * @Description TODO
 * @Author 耀酱
 * @Date 2023/6/4 16:12
 * @Version 1.0
 */
// 命令者模式
public class CommandPattern {
    public static void main(String[] args) {

        RemoteControl remoteControl = new RemoteControl(2);

        GarageDoor garageDoor = new GarageDoor();
        Command openGarageDoorCommand = new OpenGarageDoorCommand(garageDoor);
        Car car = new Car();
        Command startCarCommand = new StartCarCommand(car);
        openGarageDoorCommand.execute();
        startCarCommand.execute();


    }
}
// 命令的基类
interface Command {
    void execute();
}

class OpenGarageDoorCommand implements Command {
    private final GarageDoor garageDoor; // 命令执行者

    public OpenGarageDoorCommand(GarageDoor garageDoor) {
        this.garageDoor = garageDoor;
    }

    public void execute() {
        garageDoor.open();
    }
}

 class StartCarCommand implements Command {
    private final Car car; // 命令执行者

    public StartCarCommand(Car car) {
        this.car = car;
    }

    public void execute() {
        car.start();
    }
}

class RemoteControl {
    private final Command[] onCommands;

    public RemoteControl(int numberOfButtons) {
        onCommands = new Command[numberOfButtons];
    }

    public void setCommand(int slot, Command onCommand) {
        onCommands[slot] = onCommand;
    }

    public void pressOnButton(int slot) {
        onCommands[slot].execute();
    }

}